using UnityEngine;
using System.Collections;

public class PrimaryGUI : MonoBehaviour {
	int numOfPlayers = 5;
	int startingTroops = 10;
	int player = 3; 

	public Texture2D map;
	public Texture2D deploy;

	string Results = "";
	static int[] troops; // i is player
	
	int attackTarget;
	int attackAmount;
	int aidTarget;
	int aidAmount;
	string GetPlayerName(int i ){
		string playerName;
		if(i==player){
			playerName = "You";
		}else{
			playerName = "Player "+(i);
		}
		return playerName;
	}
//	bool moveSubmited;

	Rect mapRect;

	void ProcessTurn(PlayerAction[] actions){
		int[] aidTroops = new int[numOfPlayers];
		int[] attackTroops = new int[numOfPlayers];
		int[] defendTroops = new int[numOfPlayers];
		
		Results = "";
		for(int i = 0;i<numOfPlayers;i++){
			PlayerAction pa = actions[i];
			Results+= ""+GetPlayerName(i)+":\n";
			Results+=" Attacked "+GetPlayerName(pa.attackTarget)+" with "+pa.attackTroopNumber+" troops"+"\n";
			Results+=" Aided "+GetPlayerName(pa.defendTarget)+" with "+pa.DefendTroopNumber+" troops"+"\n";
			Results+=" Defended "+"with "+(troops[i]-(pa.DefendTroopNumber+pa.attackTroopNumber))+" troops"+"\n";
			
			Results+= ":\n";
			aidTroops[pa.defendTarget] += pa.DefendTroopNumber;
			attackTroops[pa.attackTarget] += pa.attackTroopNumber;
			defendTroops[i] = troops[i]-(pa.DefendTroopNumber+pa.attackTroopNumber);
		}
		for(int i = 0;i<numOfPlayers;i++){
			if(attackTroops[i]>aidTroops[i]+defendTroops[i]){
				troops[i]--;
			}
		}
		ResetTurn();
	}
	void ResetTurn(){
		attackTarget = -1;
		aidTarget = -1;
		aidAmount = 0;
		attackAmount = 0;
//		moveSubmited = false;
	}
	
	void ResetMatch(){
		troops = new int[numOfPlayers];
		for(int i = 0; i<numOfPlayers; i++){
			troops[i] = startingTroops;
		}
	}

	// Use this for initialization
	void Awake () {
		ResetMatch();
		ResetTurn();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void OnGUI(){

		GUILayout.BeginHorizontal();
		{
			GUILayout.BeginVertical();
			{


				mapRect = GUILayoutUtility.GetRect(500,500);
				//GUILayout.BeginArea(mapRect);
				
				//GUILayout.BeginHorizontal(GUILayout.Height(500));
				//GUILayout.BeginVertical();
				GUI.Box(new Rect(0,0,500,500),Results);
				//GUILayout.EndVertical();
				//GUILayout.BeginVertical();
				GUI.Box(new Rect(500,0,500,500),map);
				//GUILayout.EndVertical();
				//GUILayout.EndHorizontal();
				//GUILayout.EndArea();
				GUILayout.BeginHorizontal();
				{
					GUILayout.FlexibleSpace();
					//if(moveSubmited)GUI.enabled = false;
					if(GUILayout.Button(deploy,GUILayout.Height(50))){

						PlayerAction[] actionArray = GenerateAIMoves();

						int newAttackTarget = (attackTarget == -1) ? 0 : attackTarget;
						int newDefendTarget = (aidTarget == -1) ? 0 : aidTarget;

						PlayerAction ourAction = new PlayerAction(newAttackTarget,attackAmount,newDefendTarget, aidAmount);
						actionArray[player] = ourAction;
						//moveSubmited = true;

						ProcessTurn(actionArray);

					}
					GUI.enabled = true;
					GUILayout.FlexibleSpace();
				}
				GUILayout.EndHorizontal();
			}
			GUILayout.EndVertical();

			GUILayout.BeginVertical();
			{
				GUILayout.FlexibleSpace();
				
	//			if(moveSubmited)GUI.enabled = false;
				for(int i=0;i<numOfPlayers;i++){
					GUILayout.BeginVertical();
					{
						string playerName;
						if(i==player){
							playerName = "You";
						}else{
							playerName = "Player "+(i);
						}
						GUILayout.BeginHorizontal();
						{
							GUILayout.Label(playerName);
							GUILayout.FlexibleSpace();
							GUILayout.Label(" : ");
							GUILayout.FlexibleSpace();
							int playersTroops = troops[i];
							if(i == player)playersTroops -= aidAmount+attackAmount;
								GUILayout.Label(""+playersTroops);
						}
						GUILayout.EndHorizontal();
						if(i==player)GUI.enabled = false;
						GUILayout.BeginHorizontal();
						{
							//GUILayout.FlexibleSpace();
							if(attackTarget>-1&&attackTarget!=i)GUI.color = Color.gray;
							if(GUILayout.Button("Attack")&&aidTarget!=i){
								attackTarget = i;
							}
							GUI.color = Color.white;
							if(aidTarget>-1&&aidTarget!=i)GUI.color = Color.gray;
							if(GUILayout.Button("Aid")&&attackTarget!=i){
								aidTarget = i;
							}
							GUI.color = Color.white;
							if(aidTarget == i){
								aidAmount = (int)GUILayout.HorizontalSlider(aidAmount,0,troops[player]-attackAmount,GUILayout.Width(50));
								GUILayout.Label(""+aidAmount);
							}
							if(attackTarget == i){
								attackAmount = (int)GUILayout.HorizontalSlider(attackAmount,0,troops[player]-aidAmount,GUILayout.Width(50));
								GUILayout.Label(""+attackAmount);
							}
							GUI.color = Color.white;
							GUILayout.FlexibleSpace();
						}
						GUILayout.EndHorizontal();

						GUI.enabled = true;
					//	if(moveSubmited)GUI.enabled = false;
					}
					GUILayout.EndVertical();
				}
				GUI.enabled = true;
				GUILayout.FlexibleSpace();
			}
			GUILayout.EndVertical();
		}
		GUILayout.EndHorizontal();
		
//		if(moveSubmited){
//			GUILayout.BeginArea(mapRect);
//			GUILayout.Box("Waiting for others");
//			GUILayout.EndArea();
//		}
	}

	public PlayerAction[] GenerateAIMoves(){
		
		PlayerAction[] actions = new PlayerAction[numOfPlayers];
		
		for (int i = 0;i < numOfPlayers;i++){
			
			int attackNumber = 0;
			int defendNumber = 0;
			
			if (i!=player){
				int troopsLeft = troops[i];
				
				int attackTarget = 0;

					attackTarget = Random.Range (0,5);
					if (attackTarget>=i){
						attackTarget++;
					}

				
				int defendTarget = 0;
				
					attackTarget = Random.Range (0,5);
					if (defendTarget>=i){
					defendTarget++;
					}

				if (troopsLeft<3){
					
				} else {
					attackNumber = Random.Range (0,Mathf.Max(0,troopsLeft-2));
					int leftOverTroops = troopsLeft - attackNumber;
					defendNumber = Random.Range (0,Mathf.Max (0,leftOverTroops-2));
				}
				
				PlayerAction action = new PlayerAction(attackTarget,attackNumber,defendTarget,defendNumber);
				actions[i] = action;
			}
		}
		return actions;
	}
}
